<template>
  <view>
    <!-- 使用自定义的搜索组件 -->
    <gosearch @search="search"></gosearch>
    <!-- 搜索 -->
    <!-- 设置圆角 -->
    <!-- <view class="" style="background: #a40100;" @click="search">
      <uni-search-bar  :cancelButton="none"  :radius="100" ></uni-search-bar>
    </view> -->

    <!-- 搜索 -->
    <view class="scroll-view-container">
      <!-- 左侧的滚动视图区域 -->
      <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
        <view @click="lefthandle(index)" :class="['left-scroll-view-item ',activeIndex==index?'active':'']" v-for="(item,index) in  cateList"
          :key="index">
          {{item.cat_name}}
        </view>

      </scroll-view>
      <!-- 右侧的滚动视图区域 -->
      <scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <view class="left-scroll-view-item" v-for="(item2,index2) in  cateList[activeIndex].children" :key="index2">
          <!-- 标题结构 -->
          <view style="text-align:center;margin:10px 0" class="">
            /{{item2.cat_name}}/
          </view>
          <!-- 标题内容图片    -->
          <block v-if="item2.children">
            <view @click="goList(item3.cat_id)" style="display: inline-block;display;" v-for="(item3,index3) in item2.children"
              :key="index3">
              <image style="width:130rpx;height:130rpx;margin:0 25rpx" :src="item3.cat_icon" mode=""></image>
              <view style="text-align:center;font-size: 12px ;" class="">
                {{item3.cat_name}}=={{item3.cat_id}}
              </view>
            </view>
          </block>

          <block v-else>暂无数据~~~~</block>

        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import mixin from "../../mixin/tabBarBrage.js"
  export default {
    mixins: [mixin],
    data() {
      return {
        cateList: [],
        wh: 0,
        activeIndex: 0,
        scrollTop: 0
      };
    },
    onLoad() {
      this.getCateList()

      wx.getSystemInfo({
        success: (res) => {
          //console.log(this,3333)
          this.wh = res.windowHeight
        }
      })
    },
    methods: {
      async getCateList() {
        // 发起请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        // 判断是否获取失败
        if (res.meta.status !== 200) return uni.showToast()
        // 转存数据
        this.cateList = res.message

      },

      // 实现高亮切换显示
      lefthandle(index) {
        this.activeIndex = index
        //  每次点击显示右侧内容置顶
        this.scrollTop = this.scrollTop == 0 ? 1 : 0
      },

      //  跳转到商品列表页面
      goList(cat_id) {
        console.log(cat_id)
        uni.navigateTo({
          url: `../../subpkg/goods_list/goods_list?cat_id=${cat_id}`
        })

      },

      // 搜索
      search() {
        console.log(1234)
        //  跳转至 搜索页面
        uni.navigateTo({
          url: '../../subpkg/search/search'
        })

      }
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        line-height: 60px;
        background-color: #f7f7f7;
        text-align: center;
        font-size: 12px;

        // 激活项的样式
        &.active {
          background-color: #ffffff;
          position: relative;

          // 渲染激活项左侧的红色指示边线
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
          }
        }
      }
    }
  }
</style>
